package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
//import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
//    @Autowired
//    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        setmealPageQueryDTO.setPage((setmealPageQueryDTO.getPage()-1)*setmealPageQueryDTO.getPageSize());
        int count = setmealMapper.total(setmealPageQueryDTO);
        PageResult pageResult = new PageResult();
        pageResult.setTotal(count);
        List<SetmealVO> records = setmealMapper.pageQuery(setmealPageQueryDTO);
        pageResult.setRecords(records);
        return pageResult;
    }

    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        // 设置创建时间和更新时间
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());

        // 设置创建人和更新人
        Long currentId = BaseContext.getCurrentId();
        setmeal.setCreateUser(currentId);
        setmeal.setUpdateUser(currentId);

        // 保存套餐基本信息
        setmealMapper.saveSetmeal(setmeal);

        // 保存套餐菜品关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        log.info("setmealDishes: {}", setmealDishes);
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            // 批量插入套餐菜品关系
            setmealMapper.saveSetmealDish(setmealDishes);
        }
    }

    @Override
    public void changestatus(long status,long id) {
        log.info("status:{}",status);
        System.out.println(status);
        setmealMapper.setStatus(status,id);
    }

    @Override
    public SetmealVO getById(Long id) {
        SetmealVO setmealVO = setmealMapper.getById(id);
        setmealVO.setSetmealDishes(setmealMapper.getSetmealDishBySetmealId(id));
        return setmealVO;
    }


    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        log.info("setmeal:{}", setmeal);
        setmealMapper.update(setmeal);
        setmealMapper.deleteBySetmealId_Dish(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes != null && !setmealDishes.isEmpty()) {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmeal.getId());
            });
            setmealMapper.saveSetmealDish(setmealDishes);
        }
    }

    @Override
    public void deleteBySetmealId(List<Long> ids) {
        setmealMapper.deleteBySetmealIds(ids);
    }

    @Override
    public List<Setmeal> getBycategoryId(int categoryId) {
        return setmealMapper.getSetmealByCategoryIdStatus(categoryId);
    }

    @Override
    public boolean getSetmeal(Long id) {
        List<Setmeal> list = setmealMapper.getSetmeal(id);
        return list != null && !list.isEmpty();
    }

    @Override
    public List<SetmealVO> getSetmealVO(int categoryId) {
        return setmealMapper.getSetmealVO(categoryId);
    }

    @Override
    public List<SetmealVO> getSetmealBycategoryId(int categoryId) {
        return setmealMapper.getSetmealByCategoryId(categoryId);
    }

}
